
import mixin from '@/service/mixin';
import Pagination from '@/components/pagination';
import SelectAppList from '@/components/select-app-list';
import SwitchChannel from './components/SwitchChannel.vue';

export default {
    name: 'smsManage-autoSend',
    components: {
        Pagination,
        SelectAppList,
        SwitchChannel
    },
    mixins: [mixin],
    data() {
        return {
            form: {
                smsType: '',
                nodeName: '',
                status: '',
                appName: ''
            },
            formLabelWidth: this.$i18n.locale === 'zhCN' ? '145px' : '235px',
            rules: {},
            tableData: [],
            allTemplate: [],// 所有短信模板及节点名称
            allChannel: {
                otpProviders: [], // 验证码短信渠道
                noticeProviders: [] // 通知类短信渠道
            },
            userName: this.$storage.get('user').username,
            editDialog: {
                showEditDialog: false,
                editForm: {
                    smsType: '',
                    content: '',
                    id: ''
                },
                rules: {
                    content: [
                        {required: true, message: this.$t('verify.plsInput'), trigger: 'blur'},
                    ],
                }
            },
            showChannelDialog: false
            // channelDialog: {
            //     channelForm: {
            //         otpProviders: '',
            //         noticeProviders: '',
            //     },
            //     rules: {
            //         otpProviders: [
            //             {required: true, message: this.$t('verify.plsInput'), trigger: 'change'},
            //         ],
            //         noticeProviders: [
            //             {required: true, message: this.$t('verify.plsInput'), trigger: 'change'},
            //         ],
            //     }
            // },
        };
    },
    methods: {
        // 搜索列表数据
        search() {
            this.$refs.form.validate(val => {
                if (val) {
                    this.page.currentPage = 1;
                    this.getTableData();
                }
            });
        },
        // 获取列表数据
        getTableData() {
            const form = this.form;
            const params = {
                smsType: form.smsType,
                status: form.status,
                appName: form.appName,
                nodeName: form.nodeName,
            };
            this.$api.sms.selectTemplate(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.tableData = data;
                    this.tableData.forEach((item) => {
                        item.status = +item.status === 1;
                    });
                    this.tableMaxHeight = document.body.clientHeight - 220;
                }
            });
        },
        changeStatus(row) {
            this.$confirm(row.status === true ? this.$tc('common.isOpen') : this.$tc('common.isClose'), '', {
                confirmButtonText: this.$t('common.confirm'),
                cancelButtonText: this.$t('common.cancel'),
                type: 'warning'
            }).then(() => {
                const params = {
                    id: row.id,
                    status: row.status ? 1 : 0,
                    operatorName: this.userName
                }
                this.$api.sms.updateStatus(params).then(res => {
                    const {status, error} = res;
                    if ('1' === status && '00000000' === error) {
                        this.$message.success(this.$tc('common.successfulOperation'));
                        this.getTableData();
                    }
                });
            }).catch(() => {
                row.status = !row.status;
            });
        },
        edit(row) {
            this.editDialog.editForm = Object.assign(this.editDialog.editForm, row);
            this.editDialog.showEditDialog = true;
            this.$nextTick(() => {
                this.$refs.editForm.clearValidate();
            });
        },
        submitEdit() {
            this.$refs.editForm.validate((val) => {
                if (val) {
                    const { editForm } = this.editDialog;
                    const params = {
                        smsType: editForm.smsType,
                        content: editForm.content,
                        id: editForm.id
                    };
                    this.$api.sms.updateContent(params).then(res => {
                        const {status, error} = res;
                        if ('1' === status && '00000000' === error) {
                            this.$message.success(this.$tc('common.successfulOperation'));
                            this.getTableData();
                            this.editDialog.showEditDialog = false;
                        }
                    });
                }
            });
        },
        changeChannel() {
            // const { channelForm } = this.channelDialog;
            // channelForm.otpProviders = '';
            // channelForm.noticeProviders = '';
            this.showChannelDialog = true;
            // this.$nextTick(() => {
            //     this.$refs.channelForm.clearValidate();
            // });
        },
        // submitChangeChannel() {
        //     this.$refs.channelForm.validate((val) => {
        //         if (val) {
        //             const { channelForm } = this.channelDialog;
        //             const params = {
        //                 otpContent: channelForm.otpProviders,
        //                 noticeContent: channelForm.noticeProviders
        //             };
        //             this.$api.sms.changeSmsSendChannel(params).then(res => {
        //                 const {status, error} = res;
        //                 if ('1' === status && '00000000' === error) {
        //                     this.$message.success(this.$tc('common.successfulOperation'));
        //                     this.getTableData();
        //                     this.showChannelDialog = false;
        //                 }
        //             });
        //         }
        //     });
        // },
        getAppList(data) {
            if (data.length > 0) {
                this.form.appName = data[0].name;
                this.getTableData();
            }
        },
        selectTemplateNodeName() {
            const params = {
                appName: ''
            };
            this.$api.sms.selectTemplateNodeName(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data instanceof Array) {
                    this.allTemplate = data;
                }
            });
        },
        // 查询自动发送短信渠道
        // getMessageChannel() {
        //     this.$api.sms.getSmsSendProvider({}).then(res => {
        //         const {status, data, error} = res;
        //         if ('1' === status && '00000000' === error && data) {
        //             const { otpProviders, noticeProviders } = data;
        //             this.allChannel.otpProviders = otpProviders;
        //             this.allChannel.noticeProviders = noticeProviders;
        //         }
        //     });
        // },
    },
    created() {
        this.selectTemplateNodeName();
        // this.getMessageChannel();
    },
};
